package Controller;

public class calcPT {
	
private static int std1,std2,min1,min2,sec1,sec2,x,y,z;
	
	private static int h,m,s, day_int,month_int, year_int;
	private static String sec, min, hour, leftSide, rightSide, toAdd,day,month,year;

	public calcPT(){

	}
	

	
	public static String addDay(String date){
		//not PT --> yyyy-MM-dd
		
		
		 year=date.split("-")[0]; 	/*seperates year*/
		 month=date.split("-")[1];	/*seperates month*/
		 day=date.split("-")[2];	/*seperates day*/
		 
		 month_int=Integer.parseInt(month);
		 day_int=Integer.parseInt(day);
		 year_int=Integer.parseInt(year);
		 
		 day_int=day_int+1;
		 
		 /*if day is >31, then set back to 1 and increase the month*/
		 
		 if (day_int > 31){
			 month_int++;
			 day_int=1;
			 
			 /*if month is >12, then set back to 1 and increase the year*/
			  if (month_int > 12){
				  year_int++;
				  month_int=1;
			  }
		 }
		 
		 if (day_int<10){
				day="0"+Integer.toString(day_int);
			} else {
				day=Integer.toString(day_int);
			}
			
			if (month_int<10){
				month="0"+Integer.toString(month_int);
			} else {
				month=Integer.toString(month_int);
			}
			
			year=Integer.toString(year_int);
		 
		
		return year+"-"+month+"-"+day;
	}
	
	
	 public static String migrateBack(String time){
		 System.out.println(time);
		
		 h=Integer.parseInt(time.split("PT|H|M|S")[1]);/*seperates hours*/
		 m=Integer.parseInt(time.split("PT|H|M|S")[2]);/*seperates minutes*/
		 s=Integer.parseInt(time.split("PT|H|M|S")[3]);/*seperates seconds*/
		 
		
		 /*if seconds >60, then set back and increase minutes*/
		 if(s>=60){
			 s=s-60;
			 m=m+1;
		 }
		 
		 if (s<10){
			 sec="0"+Integer.toString(s);
		 } else{
			 sec= Integer.toString(s);
		 }
		 /*if minutes >60, then set back and increase hours*/
		 if(m>=60){
			 m=m-60;
			 h=h+1;
		 }
		 
		 if(m<10){
			 min="0"+Integer.toString(m);
		 } else {
			 min=Integer.toString(m);
		 }
		 
		 if(h<10){
			 hour="0"+Integer.toString(h);
		 } else{ if (h>24) {
		    	 h=h-16;
		     }
		 }
		 hour=Integer.toString(h);
		 
		// System.out.println(hour+":"+min+":"+sec);
		 return (hour+":"+min+":"+sec);
	 }
	
	 public static String migrate(String time){
		 /*migrate hh:mm:ss to PTxHxMxS*/
		 return ("PT"+time.split(":")[0]+"H"+time.split(":")[1]+"M"+time.split(":")[2]+"S");
	 }
	
	
	 public static String overrideDate(String toOverride, String addThis){
	//	 System.out.println(toOverride);

		 
		 
		 leftSide=toOverride.split("T")[0];
		 rightSide=toOverride.split("T")[1];
		 toAdd=addThis;
		 
		 year=leftSide.split("-")[0];
		 month=leftSide.split("-")[1];
		 day=leftSide.split("-")[2];
		 
		 month_int=Integer.parseInt(month);
		 day_int=Integer.parseInt(day);
		 
		 System.out.println(leftSide);
		 System.out.println(rightSide);
		 System.out.println(addThis);
		 
		 
		 
		 std1=Integer.parseInt(rightSide.split(":")[0]);
		 std2=Integer.parseInt(toAdd.split(":")[0]);
		 min1=Integer.parseInt(rightSide.split(":")[1]);
		 min2=Integer.parseInt(toAdd.split(":")[1]);
		 sec1=Integer.parseInt(rightSide.split(":")[2]);
		 sec2=Integer.parseInt(toAdd.split(":")[2]);
		 
		 		 x=std1+std2; 
				 y=min1+min2; 
				 z=sec1+sec2; 
				 
		if (z>=60){
			y++;
			z=z-60;
		}
		
		if (y>=60){
			x++;
			y=y-60;
		}
		
		if (x>=24){
			day_int=day_int+1;
			x=x-24;
		}	
			if (day_int<10){
				day="0"+Integer.toString(day_int);
			} else {
				day=Integer.toString(day_int);
			}
			
			if (month_int<10){
				month="0"+Integer.toString(month_int);
			} else {
				month=Integer.toString(month_int);
			}
			
			if (z<10){
				sec="0"+Integer.toString(z);
			} else {
				sec=Integer.toString(z);
			}
			
			if (y<10){
				min="0"+Integer.toString(y);
			} else {
				min=Integer.toString(y);
			}
			
			if (x<10){
				hour="0"+Integer.toString(x);
			} else {
				hour=Integer.toString(x);
			}
			
			
		//	System.out.println("1 "+toOverride);
		//	System.out.println("2 "+year+"-"+month+"-"+day+"T"+hour+":"+min+":"+sec);
		
				 
		 
		 return (year+"-"+month+"-"+day+"T"+hour+":"+min+":"+sec);
	 }
	 
	 
	public static String calculate(String t1, String t2, String op) {
		
		
		
		
		std1=Integer.parseInt(t1.split("(T)|(H)|(S)")[1]);
		std2=Integer.parseInt(t2.split("(T)|(H)|(S)")[1]);
		min1=Integer.parseInt(t1.split("(H)|(M)|(S)")[1]);
		min2=Integer.parseInt(t2.split("(H)|(M)|(S)")[1]);
		sec1=Integer.parseInt(t1.split("(H)|(M)|(S)")[2]);
		sec2=Integer.parseInt(t2.split("(H)|(M)|(S)")[2]);
		

	switch(op){
	
	case "-": 
		x=std1-std2;
		if (x<0){
			System.out.println("std < 0");
		}
		
		y=min1-min2;
		if (y<0){
			y=60+y;
			x=x-1;
		}
		
		z=sec1-sec2;
		if (z<0){
			z=60+z;
			y=y-1;
			 if(y<0){
				 y=60+y;
				 x=x-1;
			 }
		}
		
		if ((z<0) & (y==0) ){
			x=x-1;
		}
		
		
		break;
	

	case "+": 
		
		x=std1+std2;
		if (x>24){
			
		}
		
		y=min1+min2;
		if (y>60){
			y=y-60;
			x=x+1;
		}
		z=sec1+sec2;
	if (z>60){
		z=z-60;
		y=y+1;
	}
	


	
	break;
	
	}
		
		
		
		return ("PT"+x+"H"+y+"M"+z+"S");
	}
	
	
}
